﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class UserInfoManager
    {
        public UserInfoManager()
        { 
        }
        private static UserInfoManager instace;
        public static UserInfoManager Instacne
        {
            get
            {
                if (instace == null)
                {
                    instace = new UserInfoManager();
                    instace.LoadData();
                }
                return instace;
            }
        }
        Dictionary<string, UserInfo> userInfoCaches = new Dictionary<string, UserInfo>();
        public void LoadData()
        {
            userInfoCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<UserInfo>();
            IList<UserInfo> userInfoList = null;
            userInfoList = crt.List<UserInfo>();
            if (userInfoList != null)
            {
                foreach (UserInfo productComment in userInfoList)
                {
                    userInfoCaches.Add(productComment.ID, productComment);
                }
            }
        }
        /// <summary>
        /// 根据ID获取用户信息
        /// </summary>
        /// <param name="sId">Id</param>
        /// <returns>用户信息</returns>
        public UserInfo GetUserInfoById(string sId)
        {
            if (userInfoCaches.ContainsKey(sId))
            {
                return userInfoCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的用户信息
        /// </summary>
        /// <returns></returns>
        public UserInfo[] GetUserInfos()
        {
            return userInfoCaches.Values.ToArray();
        }
        /// <summary>
        /// 获取总的用户数
        /// </summary>
        /// <returns></returns>
        public int GetTotalUsers()
        {
            return userInfoCaches.Values.Count;
        }
        /// <summary>
        /// 获取所有的男性（女性）用户
        /// </summary>
        /// <param name="sIsMale">是否男性</param>
        /// <returns></returns>
        public UserInfo[] GetUserInfoBySex(bool sIsMale)
        {
            List<UserInfo> userInfoList = new List<UserInfo>();
            foreach (UserInfo userInfo in userInfoCaches.Values)
            {
                if (userInfo.Sex == sIsMale)
                {
                    userInfoList.Add(userInfo);
                }
            }
            return userInfoList.ToArray();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteProdcutCommentByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            UserInfo userInfo = GetUserInfoById(sId);
            if (userInfo != null)
            {
                session.Delete(userInfo);
                userInfoCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="productComment"></param>
        public void InsertProdcutComment(UserInfo userInfo)
        {
            NHHelper.SaveOjb(userInfo);
            userInfoCaches.Add(userInfo.ID, userInfo);
        }
    }
}
